package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.yw.YwFeesSubMapper;
import com.zjdiepu.www.ibiz.yw.IYwFeesSubService;
import com.zjdiepu.www.model.yw.dto.YwFeesSub;
import com.zjdiepu.www.model.yw.ext.YwFeesSubExt;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年06月30日 17时58分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwFeesSubServiceImpl extends BaseService implements IYwFeesSubService {
	public static final Logger logger = LoggerFactory.getLogger(YwFeesSubServiceImpl.class);

	@Autowired
	private YwFeesSubMapper ywFeesSubMapper;
	@Override
	public List<Map<String, Double>> countReceiveByBusiId(String busiId,
			List<String> excludeItemCodes) {
		return ywFeesSubMapper.countReceiveByBusiId(busiId, excludeItemCodes);
	}
	@Override
	public List<Map<String, Double>> countPaymentByBusiId(String busiId,
			List<String> includeItemCodes) {
		return ywFeesSubMapper.countPaymentByBusiId(busiId, includeItemCodes);
	}
	
	@Override
	public void saveUpdateAndDelete(YwFeesSub ywFeesSub, OperateType operType) {
		switch (operType) {
		case add:
			ywFeesSub.initInsertData(ywFeesSub.getModifier(), ywFeesSub.getModifierName());
			ywFeesSubMapper.insert(ywFeesSub);
			break;
		case edit:
			ywFeesSub.initUpdateData(ywFeesSub.getModifier(), ywFeesSub.getModifierName());
			// 影响的记录数
			int updateCount = ywFeesSubMapper.updateByPrimaryKeySelective(ywFeesSub);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywFeesSub.initDeleteData(ywFeesSub.getModifier(), ywFeesSub.getModifierName());
			// 影响的记录数
			int deleteCount = ywFeesSubMapper.updateByPrimaryKeySelective(ywFeesSub);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwFeesSub> selectByExt(YwFeesSubExt ywFeesSubExt) {
		return ywFeesSubMapper.selectByExt(ywFeesSubExt);
	}
	
	@Override
	public int countByExt(YwFeesSubExt ywFeesSubExt){
		return ywFeesSubMapper.countByExt(ywFeesSubExt);
	}
	
	@Override
	public YwFeesSub selectById(String id) {
		return ywFeesSubMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywFeesSubMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwFeesSub ywFeesSub) {
		return ywFeesSubMapper.countRefined(ywFeesSub);
	}

	@Override
	public YwFeesSub selectRefined(YwFeesSub ywFeesSub) {
		return ywFeesSubMapper.selectRefined(ywFeesSub);
	}
	
	@Override
	public int deletedRefined(YwFeesSub ywFeesSub) {
		return ywFeesSubMapper.deletedRefined(ywFeesSub);
	}

	@Override
	public boolean repeat(YwFeesSub ywFeesSub) {
		int count = countRefined(ywFeesSub);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywFeesSub.getId())) {
			return true;
		}
		YwFeesSub dbItem = selectRefined(ywFeesSub);
		if(null == dbItem) {
			return false;
		}
		return !ywFeesSub.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywFeesSubMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwFeesSub> ywFeesSubs) {
		return ywFeesSubMapper.insertBatch(ywFeesSubs);
	}
}
